/**
 * JFunny - A funny programming language
 * Copyright (C) 2013  fanguangping
 * Any problem email me: fguangping@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.jfunny.core.parser;

import java.util.ArrayList;
import java.util.List;

import org.jfunny.core.ds.StatementMapping;
import org.jfunny.core.util.StringUtil;

/**
 * 解析泛语言模板的类
 * 
 * @author fanguangping
 * 
 * 
 */
public class ParserTpl {
	public ParserTpl(String source) {
		this.source = source;
	}

	/**
	 * 目前模板暂由 "statement:" 和 "->" 作为分隔符
	 * @return
	 */
	public List<StatementMapping> parseTemplate() {
		List<StatementMapping> statements = new ArrayList<StatementMapping>();

		String[] srcs = StringUtil.removeComments(source, "$").split("statement:");
		for (String src : srcs) {
			String trimSrc = (src == null) ? null : src.trim();
			if (trimSrc != null && !"".equals(trimSrc)) {
				String[] trimSrcs = trimSrc.split("->");

				StatementMapping mapping = new StatementMapping();
				mapping.setRegexKey(trimSrcs[0].trim());
				mapping.setRegexValue(trimSrcs[1].trim());
				mapping.constructDfa();

				statements.add(mapping);
			}
		}

		return statements;
	}

	private String source;
}
